package com.qf.controller;

import com.qf.entiy.Health;
import com.qf.service.HealthService;
import com.qf.service.Impl.HealthServiceImpl;
import com.qf.util.Constants;
import com.qf.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
    Dell
    2021/9/30
    13:14
*/
@WebServlet("/health")
public class HealthController extends BaseController {
    public String showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageIndex = request.getParameter("pageIndex");
        if(pageIndex==null){
            pageIndex = "1";
        }
        Page page  = new Page(Integer.valueOf(pageIndex));
        HealthService healthService = new HealthServiceImpl();
        List<Health> healthList = healthService.showAllHealth(page);
        request.setAttribute("healthList", healthList);
        request.setAttribute("page", page);
        return Constants.FORWARD+"/list.jsp";
    }
    public String alter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String organId = request.getParameter("organId");
        String projectname = request.getParameter("projectname");
        String date = request.getParameter("date");
        String money = request.getParameter("money");
        String status = request.getParameter("status");
        Date deadline = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            deadline = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Health health = new Health(0, projectname, Integer.valueOf(status), Double.valueOf(money), deadline, null);
        HealthService healthService = new HealthServiceImpl();
        if(id==""){
            healthService.addHealth(health, Integer.valueOf(organId));
        }else{
            health.setId(Integer.valueOf(id));
            healthService.updateHealth(health, Integer.valueOf(organId));
        }
        return Constants.REDIRECT+"/health?method=showAll";
    }
    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        HealthService healthService = new HealthServiceImpl();
        healthService.deleteHealth(Integer.valueOf(id));
        return Constants.REDIRECT+"/health?method=showAll";
    }
}
